package com.angular.boot.controller;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.angular.aspect.LoginedValidateAspect;
import com.angular.entity.ResponseBody;
import com.angular.entity.ResponseList;
import com.angular.entity.UpdateUserParam;
import com.angular.mapper.CideUserMapper;
import com.angular.mapper.UserMapper;
import com.angular.pojo.CideUser;
import com.angular.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.StringUtil;
import com.mysql.fabric.Response;
@RestController()
@RequestMapping("/api")
public class HelloWorldController {
	@Autowired
	UserMapper userMapper;
	@Autowired
	CideUserMapper cideUserMapper;
	@Autowired
	UserService userService;

  @RequestMapping("/hello1")
  public Object hello1() {
    return "123";
  }

  @RequestMapping("/hello2")
  public List<String> hello2() {
    return Arrays.asList(new String[] { "A", "B", "C" });
  }
  @RequestMapping("/permission")
  public ResponseBody<List<Object>> getPermission() throws IOException{
	  BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("F:\\wkh\\resource\\web-browser\\angular2\\project\\angular-cli-project\\src\\app\\json\\permission.json")));
	  String permissionJsonString="";
	  
	  String tmpString;
	  while((tmpString=br.readLine())!=null) {
		  permissionJsonString+=tmpString;
	  }
	  br.close();
	  List<Object> list=null;
		list=JSON.parseArray(permissionJsonString,Object.class);
		
	  
	  
	  
	  
		
    return new ResponseBody<List<Object>>(0, "",list);
	  
	  
	  
  }
  @RequestMapping("/login")
  public ResponseBody<List<Object>> login(@RequestBody CideUser user,HttpServletRequest request) throws IOException {
	  if(StringUtil.isEmpty(user.getAccount())) {
		  user.setAccount("wkh");
		  user.setPassword("e10adc3949ba59abbe56e057f20f883e");
	  }
    return userService.login(user,request);
  }
  /**
   * 因为有切面 验证登录,所以只需要摆个空方法在这儿就是了
   * @return
   * @throws IOException
   */
  @RequestMapping("/isLogined")
  public ResponseBody<Object> isLogined() throws IOException {
    return new ResponseBody<Object>(0, "",null);
  }
  @RequestMapping("/getUsers/{pageNum}/{pageSize}")
  public ResponseBody<ResponseList<CideUser>> isLogined(@PathVariable int pageNum,@PathVariable int pageSize) throws IOException {
      //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
	  PageHelper.startPage(pageNum,pageSize);
	  List<CideUser> allItems = cideUserMapper.selectAll();        //全部商品
	  Page<CideUser> pageItems = (Page<CideUser>)allItems;        //全部商品
    return new ResponseBody<ResponseList<CideUser>>(0, "",new ResponseList<CideUser>(pageItems.getPages(), (int) pageItems.getTotal(),allItems));
  }
  
  @RequestMapping("/delete")
  public ResponseBody<Object> deleteUserByAccounts(@RequestBody List<String> accounts){
	  userService.deleteUserByAccounts(accounts);
	  return new ResponseBody<Object>(0, null, null);
  }
  @RequestMapping("/update")
  public ResponseBody<Object> updateUserByAccounts(@RequestBody UpdateUserParam updateUserParam){
	  userService.updateUserByAccounts(updateUserParam.getAccounts(),updateUserParam.getUser());
	  return new ResponseBody<Object>(0, null, null);
  }
  
  
}